unit uPesqTecnicos;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, DBGrids, ToolWin, ComCtrls, DB, IBQuery, ActnList,
  System.Actions;

type
  TPesqTecnicos_F = class(TForm)
    tbTOP: TToolBar;
    Grid: TDBGrid;
    pesqEdit: TEdit;
    Info_Pesquisa: TToolBar;
    AcPesquisa: TActionList;
    Sair: TAction;
    Seleciona: TAction;
    DsPesquisa: TDataSource;
    procedure pesqEditKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure GridDrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure GridKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure GridEnter(Sender: TObject);
    procedure SairExecute(Sender: TObject);
    procedure SelecionaExecute(Sender: TObject);
    procedure GridKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  PesqTecnicos_F: TPesqTecnicos_F;

implementation

uses
  mdsrv_u, uFunctions;

{$R *.dfm}

procedure TPesqTecnicos_F.GridDrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
GridSelect((Sender as TDBGrid),State,column);
(Sender as TDBGrid).DefaultDrawColumnCell(rect,DataCol,Column,State);
end;

procedure TPesqTecnicos_F.GridEnter(Sender: TObject);
begin
   pesqEdit.Text:=(Sender as TDBGrid).DataSource.DataSet.FieldByName('TEC_NOME').AsString;
end;

procedure TPesqTecnicos_F.GridKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin


  if key = vk_return then
    Begin
      if not (Sender as TDBGrid).DataSource.DataSet.IsEmpty then
        Seleciona.Execute;
    End;
end;

procedure TPesqTecnicos_F.GridKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  pesqEdit.Text:=(Sender as TDBGrid).DataSource.DataSet.FieldByName('TEC_NOME').AsString;

end;

procedure TPesqTecnicos_F.pesqEditKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  Info_Pesquisa.Caption := '';

  if (Key in [vk_next,vk_down]) and (not DsPesquisa.DataSet.IsEmpty) then
    Grid.SetFocus;

  if (key = vk_return) then
    Begin
      if IsInteger( trim((Sender as TEdit).Text) ) then
        Begin
          ReConnectServ;
          (DsPesquisa.DataSet as TIBQuery).Close;
          (DsPesquisa.DataSet as TIBQuery).SQL.Text := 'SELECT * FROM TECNICOS WHERE TEC_ID = '''+IntToStr(StrToInt((Sender As TEdit).Text))+'''';
          (DsPesquisa.DataSet as TIBQuery).Prepare;
          (DsPesquisa.DataSet as TIBQuery).Open;
        End
      else
        Begin
          ReConnectServ;
          (DsPesquisa.DataSet as TIBQuery).Close;
          (DsPesquisa.DataSet as TIBQuery).SQL.Text:= 'SELECT * FROM TECNICOS WHERE TEC_NOME LIKE '+QuotedStr('%'+trim((Sender As TEdit).Text)+'%');
          (DsPesquisa.DataSet as TIBQuery).Prepare;
          (DsPesquisa.DataSet as TIBQuery).Open;
        End;




    if DsPesquisa.DataSet.IsEmpty then
      Begin
        Info_Pesquisa.Caption := ' Nenhum Registro Encontrado ';
        pesqEdit.SetFocus;
      End
    else
      Grid.SetFocus;


    End;

end;

procedure TPesqTecnicos_F.SairExecute(Sender: TObject);
begin
  Self.ModalResult := mrCancel;
end;

procedure TPesqTecnicos_F.SelecionaExecute(Sender: TObject);
begin
  Self.ModalResult := mrOK;
end;

end.
